package com.sgb.zhaocai.view.demand.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import com.google.android.material.tabs.TabLayout;
import com.sgb.zhaocai.R;
import com.sgb.zhaocai.databinding.ActivityDemandIssueListBinding;
import com.sgb.zhaocai.entity.PublishListBean;
import com.sgb.zhaocai.view.demand.adapter.DemandIssueAdapter;
import com.sgb.zhaocai.view.demand.fragment.DemandIssueFragment;
import com.swgk.core.base.AppActivity;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import com.sgb.zhaocai.viewmodel.DemandIssueListModel;
import java.util.ArrayList;
import java.util.List;

/**
 * Time:2021/9/16
 * AUTHOR:F_Q
 * Description:
 */
public class DemandIssueListActivity extends AppActivity {

    private ActivityDemandIssueListBinding mBinding;
    private DemandIssueAdapter demandissueadapter;
    private DemandIssueListModel mModel;
    private ArrayList<Fragment> mFragments;
    public static int DEMANDISSUE = 22;
    String SearchName="";
    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, DemandIssueListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_demand_issue_list);
        mModel = new ViewModelProvider(this).get(DemandIssueListModel.class);
        mBinding.includeTitle.ivBack.setOnClickListener(v -> finish());
        mBinding.includeTitle.titleView.setText("需求列表");
        mBinding.includeTitle.rightView.setText("发布");
        //发布按钮点击
        mBinding.includeTitle.rightView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(getApplicationContext(), DemandIssueActivity.class).putExtra("type", 2), DEMANDISSUE);
            }
        });
        //搜索
        mBinding.tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentListData();
            }
        });
        mBinding.editSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
               if (s.toString().length()>1){
                   mBinding.searchDelete.setVisibility(View.VISIBLE);
               }else {
                   mBinding.searchDelete.setVisibility(View.GONE);
               }
            }
        });
        mBinding.searchDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBinding.editSearch.setText("");
                mBinding.searchDelete.setVisibility(View.GONE);
                FragmentListData();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mModel.getTabData(DemandIssueListActivity.this);
    }

  /*  @Override
    protected void init() {
        super.init();

    }
*/
    public void setTabList(List<PublishListBean> dataTabSecond) {
        if (null == dataTabSecond||dataTabSecond.size()==0) {
            return;
        }
        String[] mTitles = new String[dataTabSecond.size()];
        mFragments = new ArrayList<>();
        for (int i = 0; i < dataTabSecond.size(); i++) {//类型：商品集采、机械集采、设备集采…………
            mTitles[i]=(dataTabSecond.get(i).getName());
            mFragments.add(DemandIssueFragment.newInstance(dataTabSecond.get(i).getNo(),SearchName));
        }
        SlidingFragmentPagerAdapter mPagerAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, mTitles);
        mBinding.viewPager.setOffscreenPageLimit(mTitles.length);
        mBinding.viewPager.setAdapter(mPagerAdapter);
        mBinding.tabLayout.setupWithViewPager(mBinding.viewPager);
        mBinding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == DEMANDISSUE) {
              //  init();
            }
        }
    }

    private void FragmentListData() {
        Fragment fragment = mFragments.get(mBinding.viewPager.getCurrentItem());
        if (fragment!=null&&fragment instanceof DemandIssueFragment) {
            ((DemandIssueFragment) fragment).initializePop(TextUtils.isEmpty(mBinding.editSearch.getText().toString().trim())?"":mBinding.editSearch.getText().toString().trim());
        }
    }

}
